unit uFornecedores;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ComCtrls, Vcl.DBCtrls, Vcl.ExtCtrls,
  Vcl.Grids, Vcl.DBGrids, Vcl.StdCtrls, JvExStdCtrls, JvButton, JvCtrls, Data.DB,
  Vcl.Mask, Vcl.Buttons, JvExControls, JvSpeedButton;

type
  TfrmFornecedores = class(TForm)
    PageControlFornecedores: TPageControl;
    TabSheetConsulta: TTabSheet;
    TabSheetManutencao: TTabSheet;
    GroupBox1: TGroupBox;
    Label5: TLabel;
    Label10: TLabel;
    edtRazaoSocial: TEdit;
    edtCNPJ: TEdit;
    btnFiltrar: TJvImgBtn;
    GroupBox2: TGroupBox;
    DBGrid1: TDBGrid;
    Panel1: TPanel;
    DBNavigator: TDBNavigator;
    btnNovo: TJvImgBtn;
    btnEditar: TJvImgBtn;
    btnExcluir: TJvImgBtn;
    dsTblfor: TDataSource;
    DBRadioGroupTipo: TDBRadioGroup;
    Panel2: TPanel;
    btnOk: TBitBtn;
    TimerInicio: TTimer;
    GroupBox3: TGroupBox;
    Label2: TLabel;
    DBEdit2: TDBEdit;
    Label16: TLabel;
    DBEdit4: TDBEdit;
    Label17: TLabel;
    DBEdit11: TDBEdit;
    Label18: TLabel;
    DBEdit14: TDBEdit;
    Label19: TLabel;
    DBEdit15: TDBEdit;
    Label20: TLabel;
    DBEdit16: TDBEdit;
    Label21: TLabel;
    DBEdit17: TDBEdit;
    Label22: TLabel;
    DBEdit18: TDBEdit;
    btnCancelar: TJvSpeedButton;
    GroupBox4: TGroupBox;
    lblCnpjCaption: TLabel;
    dbedtForcnpj: TDBEdit;
    Label3: TLabel;
    DBEdit3: TDBEdit;
    Label4: TLabel;
    dbedtIE: TDBEdit;
    GroupBox5: TGroupBox;
    Label6: TLabel;
    DBEdit5: TDBEdit;
    Label7: TLabel;
    DBEdit6: TDBEdit;
    Label8: TLabel;
    DBEdit7: TDBEdit;
    Label9: TLabel;
    DBEdit8: TDBEdit;
    Label11: TLabel;
    DBEdit9: TDBEdit;
    Label12: TLabel;
    DBEdit10: TDBEdit;
    Label13: TLabel;
    DBLookupComboBox1: TDBLookupComboBox;
    GroupBox6: TGroupBox;
    Label1: TLabel;
    DBEdit1: TDBEdit;
    Label14: TLabel;
    DBEdit12: TDBEdit;
    Label15: TLabel;
    DBEdit13: TDBEdit;
    Label29: TLabel;
    Label28: TLabel;
    Label23: TLabel;
    Label24: TLabel;
    Label25: TLabel;
    Label26: TLabel;
    Label27: TLabel;
    Label30: TLabel;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormKeyPress(Sender: TObject; var Key: Char);
    procedure edtCNPJChange(Sender: TObject);
    procedure edtRazaoSocialChange(Sender: TObject);
    procedure btnFiltrarClick(Sender: TObject);
    procedure btnEditarClick(Sender: TObject);
    procedure btnNovoClick(Sender: TObject);
    procedure btnExcluirClick(Sender: TObject);
    procedure TabSheetManutencaoShow(Sender: TObject);
    procedure DBRadioGroupTipoClick(Sender: TObject);
    procedure btnOkClick(Sender: TObject);
    procedure TimerInicioTimer(Sender: TObject);
    procedure btnCancelarClick(Sender: TObject);
  private
    { Private declarations }
    procedure FiltrarFornecedores;
    procedure ControlarPageControl(APageIndex: integer);

  public
    { Public declarations }
  end;

var
  frmFornecedores: TfrmFornecedores;

implementation

{$R *.dfm}

uses uDadosFornecedores, uDadosGlobal;

procedure TfrmFornecedores.btnEditarClick(Sender: TObject);
begin
  dmDadosFornecedores.EditarFornecedor;

  ControlarPageControl(1);
end;

procedure TfrmFornecedores.btnExcluirClick(Sender: TObject);
begin
  try
    dmDadosFornecedores.ExcluirFornecedor;
  except
    on E: Exception do
    begin
      MessageDlg(e.Message, mtError,[mbOK],0);
      FiltrarFornecedores;
    end;
  end;
end;

procedure TfrmFornecedores.btnFiltrarClick(Sender: TObject);
begin
  FiltrarFornecedores;
end;

procedure TfrmFornecedores.btnNovoClick(Sender: TObject);
begin
  dmDadosFornecedores.AdicionarFornecedor;

  ControlarPageControl(1);
end;

procedure TfrmFornecedores.btnOkClick(Sender: TObject);
begin
  dmDadosFornecedores.SalvarManutencao;

  ControlarPageControl(0);
end;

procedure TfrmFornecedores.ControlarPageControl(APageIndex: integer);
begin
  if APageIndex = 0 then
  begin
    PageControlFornecedores.Pages[0].TabVisible := true;  // Tab de consulta de fornecedores
    PageControlFornecedores.Pages[1].TabVisible := false; // Tab de manutencao de fornecedores
    PageControlFornecedores.ActivePageIndex := 0;
    edtCNPJ.SetFocus;
  end
  else
  begin
    PageControlFornecedores.Pages[0].TabVisible := false; // Tab de consulta de fornecedores
    PageControlFornecedores.Pages[1].TabVisible := true;  // Tab de manutencao de fornecedores
    PageControlFornecedores.ActivePageIndex := 1;
  end;
end;

procedure TfrmFornecedores.DBRadioGroupTipoClick(Sender: TObject);
begin
  try
    dmDadosFornecedores.cdsTblforfortip.Value := DBRadioGroupTipo.ItemIndex;

    if DBRadioGroupTipo.ItemIndex = 0 then
    begin
      lblCnpjCaption.Caption := 'CPF:';
      dbedtIE.Enabled := false;
    end
    else
    begin
      lblCnpjCaption.Caption := 'CNPJ:';
      dbedtIE.Enabled := true;
    end;
  finally
    dbedtForcnpj.SetFocus;
  end;
end;

procedure TfrmFornecedores.edtCNPJChange(Sender: TObject);
begin
  FiltrarFornecedores;
end;

procedure TfrmFornecedores.edtRazaoSocialChange(Sender: TObject);
begin
  FiltrarFornecedores;
end;

procedure TfrmFornecedores.FiltrarFornecedores;
var
  SQL: String;
begin

  if trim(edtCNPJ.Text) <> EmptyStr then
    SQL := ' and forcnpj like "%' + trim(edtCNPJ.Text) + '%" ';

  if Trim(edtRazaoSocial.Text) <> EmptyStr then
    SQL := SQL + ' and forrazaosocial like "%' + trim(edtRazaoSocial.Text) + '%" ';

  SQL :=
    'select *      ' +
    '  from tblfor ' +
    ' where 1 = 1  ' +
    SQL +
    ' order by forrazaosocial';

  dmDadosFornecedores.FiltrarFornecedores(SQL);
end;

procedure TfrmFornecedores.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caFree;
  Release;
  frmFornecedores := nil;
end;

procedure TfrmFornecedores.FormKeyPress(Sender: TObject; var Key: Char);
begin
  if key = #13 Then
  begin
    key := #0;
    Perform(Wm_NextDlgCtl,0,0);
  end;
end;

procedure TfrmFornecedores.btnCancelarClick(Sender: TObject);
begin
  dmDadosFornecedores.CalcelarManutencao;

  ControlarPageControl(0);
end;

procedure TfrmFornecedores.TabSheetManutencaoShow(Sender: TObject);
begin
  if not dmDadosFornecedores.cdsTblfor.Active then
    exit;

  DBRadioGroupTipoClick(DBRadioGroupTipo);

  if dmDadosFornecedores.cdsTblfor.State = dsEdit then
  begin
    DBRadioGroupTipo.Enabled := false;
    dbedtForcnpj.ReadOnly := true;
    dbedtForcnpj.Color := clBtnFace;

    DBEdit3.SetFocus;
  end
  else if dmDadosFornecedores.cdsTblfor.State = dsInsert then
  begin
    DBRadioGroupTipo.Enabled := true;
    dbedtForcnpj.ReadOnly := false;
    dbedtForcnpj.Color := clWindow;

    dbedtForcnpj.SetFocus;
  end;
end;

procedure TfrmFornecedores.TimerInicioTimer(Sender: TObject);
begin
  TimerInicio.Enabled := false;

  ControlarPageControl(0);

  frmFornecedores.WindowState := wsMaximized;
end;

end.
